package valid

import (
	"fmt"

	"go-data-washing/utils"
)

type Middle struct {
	Valids map[string]ValidLine
}

func (m Middle) CheckAndFormat(row map[string]string) (map[string]string, bool) {

	dirtyTypes := []string{}

	for key, validLine := range m.Valids {

		current, has := row[key]

		if !has {
			continue
		}

		if current == "" {
			continue
		}

		_current := current

		for _, validNode := range validLine {

			if !validNode.Check(current) && validNode.hasFormat {
				_current = validNode.Format(current)
			}

			if !validNode.Check(_current) {
				fmt.Println("ERROR: ", current)
				// time.Sleep(time.Second * 1)
				dirtyTypes = append(dirtyTypes, key)
			} else {
				// 缓存修改成功的数据
				row[key] = _current
			}

			// 是否中断流水线
			if !validNode.next {
				break
			}

		}

	}

	if len(dirtyTypes) > 0 {
		row["dirty_types"] = utils.SetDirtyTypes(dirtyTypes)
		return row, true
	}

	row["clean"] = "1"
	row["dirty_types"] = ""
	return row, false
}

var Leve1Middle = Middle{
	map[string]ValidLine{
		"start_datetime":        TimeValidLine,
		"registration_time":     TimeValidLine,
		"end_datetime":          TimeValidLine3,
		"transaction_type":      CnValidLine,
		"subject_type":          CnValidLine,
		"native_city":           CnValidLine,
		"area_city":             CnValidLine,
		"legal_person":          CnValidLine,
		"store_url":             URLValidLine,
		"web_registration_name": URLRecordNoValidLine,
	},
}

var Leve2Middle = Middle{
	map[string]ValidLine{
		"start_datetime":    TimeValidLine2,
		"registration_time": TimeValidLine2,
		"end_datetime":      TimeValidLine3,
	},
}
